package web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import vo.User;
import business.UserService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class UserAction extends ActionSupport implements Preparable {
	private UserService service;

	private User curUser;
	private List<User> users;
	private ArrayList<String> delIds = new ArrayList<String>();

	// 分页的变量
	private int curPage;
	private int totalPage;
	private int pageSize = 5;

	private String newPSW;

	/** 新增用户参数 */
	private String username;
	private String password;
	private String mobile;
	private String phone;
	private String email;
	private String department;
	private String name;

	private String errorMsg;

	public String logout() {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		session.clear();
		System.out.println("clear session");
		return Action.SUCCESS;

	}

	public String listUser() {
		System.out.println("curPage:" + curPage);
		int totalCount = (int) service.getTotalCount();
		System.out.println("totalCount:" + totalCount);
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("totalCount / pageSize:" + totalCount / pageSize);

		if (curPage == 0)
			curPage = 1;
		this.users = service.getAllUsers((curPage - 1) * pageSize, pageSize);
		System.out.println("user count:" + this.users.size());
		return Action.SUCCESS;
	}

	public String addUser() {
		service.addUser("hello xxx");
		return listUser();
	}

	public String deleteUser() {
		System.out.println("delete user:");
		for (int i = 0; i < this.delIds.size(); i++) {
			System.out.println("deleted ids：" + delIds.get(i));
			long userId = Long.parseLong(delIds.get(i));


			service.delete(userId);
		}
		return "userDeleted";
	}
	public List<User> getUsers() {
		return users;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = service.getByName(username);
	}

	public void setUserService(UserService service) {
		this.service = service;
	}

	public void setService(UserService service) {
		this.service = service;
	}

	public ArrayList<String> getDelIds() {
		return delIds;
	}

	public void setDelIds(ArrayList<String> delIds) {
		this.delIds = delIds;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getNewPSW() {
		return newPSW;
	}

	public void setNewPSW(String newPSW) {
		this.newPSW = newPSW;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

}